/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import pojo.Chitietkhuyenmai;
import pojo.Khuyenmai;
import pojo.Sanpham;
import util.HibernateUtil;

/**
 *
 * @author Cong
 */
public class daoKhuyenMai {
    
    public static List<Khuyenmai> LaydsKM() {
        List<Khuyenmai> ds = null;
        String sql = "from Khuyenmai order by id asc";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static boolean ThemCT(Khuyenmai km) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.save(km);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean XoaCT(Khuyenmai km) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.delete(km);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean LuuCT(Khuyenmai km) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.update(km);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static List<Object[]> GetSPKM(Khuyenmai km) {
        List<Object[]> ds = null;
        String sql = "SELECT ctkm.id, sp.masanpham, sp.tensanpham, ctkm.loai "
                + "FROM Chitietkhuyenmai ctkm, Khuyenmai km, Sanpham sp "
                + "WHERE km.id = ctkm.khuyenmai.id "
                + "AND sp.id = ctkm.sanpham.id AND km.id = :id order by ctkm.id asc";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            qr.setParameter("id", km.getId());
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static List<Sanpham> GetdsSP() {
        List<Sanpham> ds = null;
        String sql = "from Sanpham";
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Query qr = sS.createQuery(sql);
            ds = qr.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static List<Khuyenmai> FindKM(Khuyenmai km) {
        List<Khuyenmai> ds = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria cR = sS.createCriteria(Khuyenmai.class);
            cR.add(Restrictions.ilike("tenchuongtrinh", "%" + km.getTenchuongtrinh() + "%"));
            if (km.getNgaybatdau() != null) {
                cR.add(Restrictions.eq("ngaybatdau",km.getNgaybatdau()));
            }
            if (km.getNgayketthuc() != null) {
                cR.add(Restrictions.eq("ngayketthuc",km.getNgayketthuc()));
            }
            ds = cR.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static List<Chitietkhuyenmai> FindCTKM(Chitietkhuyenmai ctkm) {
        List<Chitietkhuyenmai> ds = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria cR = sS.createCriteria(Chitietkhuyenmai.class);
            cR.add(Restrictions.eq("sanpham", ctkm.getSanpham()));
            cR.add(Restrictions.eq("khuyenmai", ctkm.getKhuyenmai()));
            cR.add(Restrictions.eq("loai", ctkm.getLoai()));
            ds = cR.list();
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return ds;
    }
    
    public static boolean ThemCTKM(Chitietkhuyenmai ctkm) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.save(ctkm);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static Chitietkhuyenmai LayThongTinCTKM(Chitietkhuyenmai ctkm) {
        Chitietkhuyenmai kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            kq = (Chitietkhuyenmai)sS.get(Chitietkhuyenmai.class, ctkm.getId());
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean XoaCTKM(Chitietkhuyenmai ctkm) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.delete(ctkm);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static Khuyenmai LayThongTinKM(Khuyenmai km) {
        Khuyenmai kq = null;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        try {
            kq = (Khuyenmai)sS.get(Khuyenmai.class, km.getId());
        } catch (HibernateException he) {
            System.err.println(he);
        } finally {
            sS.close();
        }
        return kq;
    }
    
    public static boolean LuuCTKM(Chitietkhuyenmai ctkm) {
        boolean kq = true;
        Session sS = HibernateUtil.getSessionFactory().openSession();
        Transaction tS = null;
        try {
            tS = sS.beginTransaction();
            sS.update(ctkm);
            tS.commit();
        } catch (HibernateException he) {
            tS.rollback();
            System.err.println(he);
            kq = false;
        } finally {
            sS.close();
        }
        return kq;
    }
}
